package com.longrise.android.byjk.plugins.tabthird;

import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.common.utils.AppUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by callxkj on 2017/9/29.
 * 我的课程的adapter
 */

public class MineCourseAdapter extends BaseQuickAdapter<EntityBean, BaseViewHolder> {

    private List<EntityBean> mListBeans = new ArrayList<>();
    private OnItemClickListener mListener;


    public MineCourseAdapter() {
        super(R.layout.item_training_management_my);
    }


    public void refreshData(EntityBean[] beans) {
        mListBeans.clear();
        for (EntityBean bean : beans) {
            mListBeans.add(bean);
        }
        setNewData(mListBeans);
    }

    @Override
    protected void convert(BaseViewHolder helper, final EntityBean item) {
        final int position = helper.getLayoutPosition();
        final View convertView = helper.getConvertView();
        ImageView iv = (ImageView) convertView.findViewById(R.id.item_train_my_iv);
        final TextView tvTitle = (TextView) convertView.findViewById(R.id.item_train_my_tv1);
        TextView tvDate = (TextView) convertView.findViewById(R.id.item_train_my_tv2);


        EntityBean typeBean2 = mListBeans.get(position - 1);
        if (typeBean2 != null) {
            String realpic = typeBean2.getString("realpic", "");
            Glide.with(mContext)
                    .load(realpic)
                    .placeholder(mContext.getResources().getDrawable(R.drawable.img03))
                    .crossFade()
                    .into(iv);
            Glide.get(mContext).clearMemory();

            //------标题---------
            String name = typeBean2.getString("name", ""); //name
            tvTitle.setText(name == null ? "" : name);

            tvTitle.post(new Runnable() {
                @Override
                public void run() {
                    int lineCount = tvTitle.getLineCount();
                    ViewGroup.LayoutParams layoutParams = convertView.getLayoutParams();
                    if (lineCount == 2) {
                        layoutParams.height = AppUtil.dip2px(126);
                    } else {
                        layoutParams.height = AppUtil.dip2px(108);
                    }
                    convertView.setLayoutParams(layoutParams);
                }
            });

            //有效时间
            String expiretime = typeBean2.getString("expiretime", "");//expiretime
            tvDate.setText(expiretime == null || expiretime.equals("") ? "" : "有效期至\u0020" + expiretime.substring(0, 10));

            //是否通过
            boolean ispass = typeBean2.getBoolean("ispass");
            helper.setText(R.id.item_train_my_status_tv, ispass ? "已通过" : "未通过");
            TextView tvstatus = helper.getView(R.id.item_train_my_status_tv);
            tvstatus.setTextColor(ispass ? Color.parseColor("#D8D8D8") : Color.parseColor("#FBA140"));

            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onClick(item);
                    }
                }
            });

        }

    }


    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    public interface OnItemClickListener {
        void onClick(EntityBean bean);
    }
}
